#include <stdio.h>
#include <string.h>
#include "geminai_mqtt_cache.h"
#include "geminai_para.h"
#include "cJSON.h"
#include "geminai_oss.h"
extern base_station_t base_station;
static char *rmei = base_station.imei;
static int year;
static int mon;
double angle = 35.441406;
static char cur_path[128] = {0};
static char cur_jpg_name[128] = {0};
static char cur_jpg_url[128] = {0};


void get_client_sn(char *client_sn, size_t client_sn_size)
{
    // 获取当前时间
    struct tm timeinfo;
    time_t now;

    // 获取当前时间戳
    time(&now);
    // 将时间戳转换为本地时间
    localtime_r(&now, &timeinfo);

    // 提取年月日时分秒
    int year = timeinfo.tm_year + 1900; // tm_year 从 1900 年开始
    int mon = timeinfo.tm_mon + 1;      // tm_mon 从 0 开始
    int data = timeinfo.tm_mday;        // tm_mday 从 1 开始
    int hour = timeinfo.tm_hour;        // tm_hour 从 0 开始
    int min = timeinfo.tm_min;          // tm_min 从 0 开始
    int sec = timeinfo.tm_sec;          // tm_sec 从 0 开始

    // 假设 imei_str 和 sn 是已定义的字符串
    const char *imei_str = base_station.imei; // 示例 IMEI 字符串
    int sn = 123456;                          // 示例序列号

    // 格式化 client_sn
    snprintf(client_sn, client_sn_size, "%.04d%.02d%.02d%.02d%.02d%.02d%s%.06d",
             year, mon, data, hour, min, sec, imei_str, sn);

    printf("client_sn : %s\n", client_sn);
}

// 序列号初始化
int sequence_number = 1;

// 获取当前的日期和时间，并格式化为所需的形式
void get_current_datetime(char *date_str, char *datetime_str)
{
    time_t rawtime;
    struct tm *timeinfo;

    time(&rawtime);
    timeinfo = localtime(&rawtime);

    // 格式化日期 (YYYYMM/DD)
    strftime(date_str, 12, "%Y%m/%d", timeinfo);

    // 格式化日期时间 (YYYYMMDDHHMMSS)
    strftime(datetime_str, 15, "%Y%m%d%H%M%S", timeinfo);
}

// 构造完整的路径字符串
void construct_path(char *path, size_t path_size, char *jpg_name, size_t jpg_name_size, char *jpg_url, size_t jpg_url_size, float angle)
{
    char date_str[12];
    char datetime_str[15];
    char sequence_str[7];

    // 获取当前日期和时间
    get_current_datetime(date_str, datetime_str);

    // 处理序列号
    sprintf(sequence_str, "%06d", sequence_number);
    sequence_number++; // 序列号递增

    // 构造路径
    snprintf(path, path_size,
             "cabinet/images/%s/%s/%s%s%06d/%s-%06d-%.6f.jpg",
             rmei, date_str, datetime_str, rmei,sequence_number - 1,datetime_str, sequence_number - 1, angle);

    snprintf(jpg_name, jpg_name_size,
             "%s-%06d-%.6f.jpg",
             datetime_str, sequence_number - 1, angle);

    snprintf(jpg_url, jpg_url_size,
             "cabinet/images/%s/%s/%s%s%06d",
             rmei, date_str, datetime_str, rmei,sequence_number - 1);

    printf("cur_jpg_name : %s\n", cur_jpg_name);
    printf("cur_jpg_url : %s\n", cur_jpg_url);
    printf("cur_jpg_name : %s\n", cur_jpg_name);
}

// int main() {
//     char path[256];

//     // 构造路径
//     construct_path(path, sizeof(path));

//     // 输出路径
//     printf("%s\n", path);

//     return 0;
// }
void build_display_photo_event_json(char *json_str_buf, size_t json_str_size, float angle)
{
    char client_sn[32];

    // 生成 client_sn
    get_client_sn(client_sn, sizeof(client_sn));
    construct_path(cur_path, sizeof(cur_path), cur_jpg_name, sizeof(cur_jpg_name), cur_jpg_url, sizeof(cur_jpg_url), angle);
    // 生成 url
    cJSON *params = cJSON_CreateObject();
    cJSON *device_result = cJSON_CreateObject();

    // 添加 params 对象的字段
    cJSON_AddStringToObject(params, "asset_id", "YQBR22003224SC-715W-R2EG");

    // 添加 ai_result 对象的字段

    cJSON_AddStringToObject(params, "client_sn", client_sn);
    cJSON_AddStringToObject(params, "ai_result", "");

    // 添加 device_result 对象的字段
    cJSON_AddStringToObject(device_result, "url", cur_path);
    cJSON_AddNumberToObject(device_result, "angle", 30.00);
    cJSON_AddStringToObject(device_result, "record_time", "1722226330802");
    cJSON_AddTrueToObject(device_result, "is_recognized");
    cJSON_AddNumberToObject(device_result, "algorithm_ret", 0);
    cJSON_AddNumberToObject(device_result, "recognize_method", 0);
    cJSON_AddNumberToObject(device_result, "recognize_status", 0);
    cJSON_AddNumberToObject(device_result, "files_from", 1);
    cJSON_AddNumberToObject(device_result, "files_total", 0);
    cJSON_AddNumberToObject(device_result, "upload_total", 0);

    char* device_result_str = cJSON_PrintUnformatted(device_result);

    cJSON_AddStringToObject(params, "device_result", device_result_str);

    free(device_result_str);
    cJSON_AddStringToObject(params, "event_time", "1722226330802");


    // 打印 JSON 字符串
    char *json_str = cJSON_PrintUnformatted(params);
    if (json_str)
    {
        printf("%s\n", json_str);
        // 将 JSON 字符串复制到输出缓冲区
        if (json_str_size > 0)
        {
            strncpy(json_str_buf, json_str, json_str_size - 1);
            json_str[json_str_size - 1] = '\0'; // 确保字符串以 null 结尾
        }
    }

    // 释放内存
    cJSON_Delete(params);
}

const char *oss_endpoint = "oss-cn-beijing.aliyuncs.com";
const char *bucket_name = "cny-static";
const char *bucket_dir = "test";
const char *access_key = "LTAI5tE8yFGUZCmtuj3KQ38V";
const char *access_securty = "cBPyrx8Ris6zMlIwTkx4EpDsEXwiEh";
const char *sts_token = NULL;
void upload_oss(char *image_buffer, int img_read)
{
    /*===========开始上传oss==========*/
    esp_err_t result = oss_upload_mem(cur_jpg_name, image_buffer, img_read, oss_endpoint, bucket_name, cur_jpg_url, access_key, access_securty, sts_token);
    printf("oss_upload file_path = %s ret = %d\n", cur_jpg_url, result);
    if (result == ESP_OK)
    {
        printf("OSS operation succeeded\n");
    }
    else
    {
        printf("OSS operation failed with error code: %d\n", result);
    }
}

void send_disp_photo_event(float angle)
{
    int ret;
    char *json_str = (char *)malloc(1024);
    build_display_photo_event_json(json_str, 1024, angle);
    ret = vision_event_report("display_event_v2", json_str);
    if (ret != 0)
    {
        printf("event report failed\n\n");
    }
}